//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MarketModelPathwiseCashRebate.h"
using namespace Cephei::QL::Models::Marketmodels::Products::Pathwise;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelPathwiseMultiProduct.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::CMarketModelPathwiseCashRebate (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, Cephei::IVector<Double>^ paymentTimes, Cephei::QL::Math::IMatrix^ amounts, UInt64 numberOfProducts) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseCashRebate::typeid)
{
    CEvolutionDescription^ _Cevolution;
    CoVector<Double>^ _CpaymentTimes;
    CMatrix^ _Camounts;
    try
    {
#ifdef HANDLE
        _phMarketModelPathwiseCashRebate = NULL;
#endif
        _Cevolution = safe_cast<CEvolutionDescription^> (evolution);
        _Cevolution->Lock();
        QuantLib::EvolutionDescription& _evolution = static_cast<QuantLib::EvolutionDescription&> (_Cevolution->GetReference ()); 
        CoVector<Double>^ _CpaymentTimes = safe_cast<CoVector<Double>^> (paymentTimes);
        _CpaymentTimes->Lock();
        INativeVector<Double>^ _NCIpaymentTimes = _CpaymentTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCpaymentTimes = safe_cast<CDoubleVector^>(_NCIpaymentTimes);
        std::vector<QuantLib::Time>& _paymentTimes = static_cast<std::vector<QuantLib::Time>&> (_NCpaymentTimes->GetReference ());
        _Camounts = safe_cast<CMatrix^> (amounts);
        _Camounts->Lock();
        QuantLib::Matrix& _amounts = static_cast<QuantLib::Matrix&> (_Camounts->GetReference ()); 
        QuantLib::Size _numberOfProducts = (QuantLib::Size)ValueHelper::Convert (numberOfProducts);
        _ppMarketModelPathwiseCashRebate = new boost::shared_ptr<QuantLib::MarketModelPathwiseCashRebate> (new QuantLib::MarketModelPathwiseCashRebate ( _evolution,  _paymentTimes,  _amounts,  _numberOfProducts ));
        SetMarketModelPathwiseMultiProduct (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseCashRebate));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cevolution != nullptr) _Cevolution->Unlock();
        if (_CpaymentTimes != nullptr) _CpaymentTimes->Unlock();
        if (_Camounts != nullptr) _Camounts->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::CMarketModelPathwiseCashRebate (boost::shared_ptr<QuantLib::MarketModelPathwiseCashRebate>& childNative, Object^ owner) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseCashRebate::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseCashRebate = NULL;
#endif
	_ppMarketModelPathwiseCashRebate = &childNative;
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseCashRebate));
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::CMarketModelPathwiseCashRebate (QuantLib::MarketModelPathwiseCashRebate& childNative, Object^ owner) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseCashRebate::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseCashRebate = NULL;
#endif
	_ppMarketModelPathwiseCashRebate = new boost::shared_ptr<QuantLib::MarketModelPathwiseCashRebate> (&childNative);
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseCashRebate));
    _MarketModelPathwiseCashRebateOwner = owner;
    _MarketModelPathwiseMultiProductOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::CMarketModelPathwiseCashRebate (CMarketModelPathwiseCashRebate^ copy) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseCashRebate::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseCashRebate = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelPathwiseCashRebate = new boost::shared_ptr<QuantLib::MarketModelPathwiseCashRebate> (copy->GetShared());
        _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseCashRebate));
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::CMarketModelPathwiseCashRebate (System::Type^ t) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseCashRebate::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseCashRebate = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelPathwiseCashRebate::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::CMarketModelPathwiseCashRebate (QuantLib::Handle<QuantLib::MarketModelPathwiseCashRebate>& childNative, Object^ owner)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseCashRebate::typeid)
{
	_phMarketModelPathwiseCashRebate = &childNative;
	_ppMarketModelPathwiseCashRebate = &static_cast<boost::shared_ptr<QuantLib::MarketModelPathwiseCashRebate>>(childNative.currentLink());
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseCashRebate));
    _MarketModelPathwiseCashRebateOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::CMarketModelPathwiseCashRebate (QuantLib::Handle<QuantLib::MarketModelPathwiseCashRebate> childNative)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseCashRebate::typeid)
{
	_phMarketModelPathwiseCashRebate = &childNative;
	_ppMarketModelPathwiseCashRebate = &static_cast<boost::shared_ptr<QuantLib::MarketModelPathwiseCashRebate>>(childNative.currentLink());
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseCashRebate));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::CMarketModelPathwiseCashRebate (QuantLib::MarketModelPathwiseCashRebate childNative)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseCashRebate::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseCashRebate = NULL;
#endif
	_ppMarketModelPathwiseCashRebate = new boost::shared_ptr<QuantLib::MarketModelPathwiseCashRebate> (new QuantLib::MarketModelPathwiseCashRebate (childNative));
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseCashRebate));
}
#endif

Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::~CMarketModelPathwiseCashRebate ()
{
    if (_ppMarketModelPathwiseCashRebate != NULL)
    {
	    delete _ppMarketModelPathwiseCashRebate;
        _ppMarketModelPathwiseCashRebate = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::!CMarketModelPathwiseCashRebate ()
{
    if (_ppMarketModelPathwiseCashRebate != NULL)
    {
	    delete _ppMarketModelPathwiseCashRebate;
    }
}
QuantLib::MarketModelPathwiseCashRebate& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::GetReference ()
{
    if (_ppMarketModelPathwiseCashRebate == NULL) throw gcnew NativeNullException ();
	return **_ppMarketModelPathwiseCashRebate;
}
boost::shared_ptr<QuantLib::MarketModelPathwiseCashRebate>& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::GetShared ()
{
    if (_ppMarketModelPathwiseCashRebate == NULL) throw gcnew NativeNullException ();
	return *_ppMarketModelPathwiseCashRebate;
}
QuantLib::MarketModelPathwiseCashRebate* Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::GetPointer ()
{
    if (_ppMarketModelPathwiseCashRebate == NULL) throw gcnew NativeNullException ();
	return &**_ppMarketModelPathwiseCashRebate;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelPathwiseCashRebate>& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::GetHandle ()
{
	if (_phMarketModelPathwiseCashRebate == NULL)
	{
		_phMarketModelPathwiseCashRebate = new Handle<QuantLib::MarketModelPathwiseCashRebate> (*_ppMarketModelPathwiseCashRebate);
	}
	return *_phMarketModelPathwiseCashRebate;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::HasNative () 
{
	return (_ppMarketModelPathwiseCashRebate != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::AlreadyDeflated::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppMarketModelPathwiseCashRebate)->alreadyDeflated ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppMarketModelPathwiseCashRebate)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelPathwiseCashRebate)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelPathwiseCashRebate)->numberOfProducts ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppMarketModelPathwiseCashRebate)->possibleCashFlowTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::IMarketModelPathwiseCashRebate^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMarketModelPathwiseCashRebate)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate::SuggestedNumeraires::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppMarketModelPathwiseCashRebate)->suggestedNumeraires ( );   
        Cephei::IVector<UInt64>^ _nrv = gcnew CoVector<UInt64> (gcnew CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Products::Pathwise::IMarketModelPathwiseCashRebate^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseCashRebate_Factory::Create (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, Cephei::IVector<Double>^ paymentTimes, Cephei::QL::Math::IMatrix^ amounts, UInt64 numberOfProducts)
{
    return gcnew CMarketModelPathwiseCashRebate ( evolution,  paymentTimes,  amounts,  numberOfProducts);
}
